package com.itheima.user.interceptor;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取所有key
        Cookie[] cookies = request.getCookies();
        if (null != cookies && 0 != cookies.length) {
            for (Cookie cookie : cookies) {
                if ("heima".equals(cookie.getName()) && !StringUtils.isEmpty(cookie.getValue())) {
                    //放行
                    return true;
                }
            }
        }
        //不放行,转发给添加cookie
        request.getRequestDispatcher("/editInfo.html").forward(request,response);
        return false;
    }
}
